using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Dao;
using Common;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using System.Data;
using System.Text;
using Model;

namespace SettingServer.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class Permit_MenuController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetMenu()
        {
            try
            {
                string Menu = OperationRedis.GetValue("Menu");
                logGuradar.writeLog("菜单");
                if (string.IsNullOrEmpty(Menu) || Menu == "null")
                {
                    DataTable menuBM = new Permit_MenuDM().Get_MenuBM();
                    if(menuBM!=null)
                    {
                       OperationRedis.StringSet("Menu", aside(menuBM, "0"), DateTime.Now.AddMinutes(30) - DateTime.Now);
                    }
                    return OperationRedis.GetValue("Menu");
                }
                else
                {
                    return Menu;
                }
            }
            catch (System.Exception ex)
            {
                return "error:" + ex.Message;
            }
        }

       
        private static string aside(DataTable data, string id)
        {
            IList<Permit_Menu> listMenu = SetUpThe.DtToList<Permit_Menu>(data);
            StringBuilder sb = new StringBuilder();
            
                IList<Permit_Menu> dat = listMenu.Where(p => p.Menu_id.ToString() == id).ToList();
                if (dat != null && dat.Count > 0)
                {
                    foreach (var menubm in dat)
                    {
                        sb.Append(@"<li> <a href='javascript:;'><i class='iconfont left-nav-li' lay-tips='" + menubm.Name + "'>&#xe6b8;</i><cite>" + menubm.Name + "</cite><i class=\"iconfont nav_right\">&#xe697;</i></a><ul class=\"sub-menu\">");
                        IList<Permit_Menu> datList = listMenu.Where(p => p.Menu_id == menubm.id).ToList();
                        foreach (var item in datList)
                        {
                            sb.Append("<li><a onclick=\"xadmin.add_tab('"+ item.Name +"','" + item.url + "')\"><i class=\"iconfont\">&#xe6a7;</i><cite>"+ item.Name +"</cite></a></li>");
                        }
                        sb.Append("</ul></li>");
                    }
                }
            return sb.ToString();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetListMenu(int page, int limit)
        {
            JsonStructureValue jsondata = new JsonStructureValue();
            int count = 0;
            jsondata.code = "0";
            List<MenuList> menuBM = new Permit_MenuDM().GetListMenu(page, limit, ref count);
            jsondata.count = count; jsondata.msg = "";
            jsondata.data = menuBM;
            string data = JsonConvert.SerializeObject(jsondata);
            return data;
        }
    }
}
